<template>
	<!-- #ifdef MP-WEIXIN  -->
	<view class="force-login-wrap">
		<!-- <image class="logo-bg" src="../static/tRefresh/fail.png" mode="aspectFill"></image> -->
		<view class="force-login__content y-f">
			<open-data class="user-avatar" type="userAvatarUrl"></open-data>
			<open-data class="user-name" type="userNickName"></open-data>
			<view class="login-notice">为了提供更优质的服务，需要获取您的头像昵称</view>
			<button class="cu-btn author-btn" @getuserinfo="getuserinfo" open-type="getUserInfo">授权并查看</button>
			<button class="cu-btn close-btn" @tap="closeAuth">暂不授权</button>
		</view>
	</view>
	<!-- #endif  -->
</template>

<script>
/**
 * 登录提示页
 * @property {Boolean} value=showLoginTip - 由v-model控制显示隐藏。
 * @property {Boolean} forceOauth - 小程序端特制的全屏登录提示。
 */

export default {
	data() {
		return {};
	},
	methods: {
		getuserinfo(e) {
			console.log(e)
			if (e.detail.errMsg === "getUserInfo:fail auth deny")
			  return uni.$showMsg("您取消了授权！");
			
			this.getToken(e.detail);
			
		},
		
		
		async getToken(info) {
		  const [err, res] = await uni.login().catch((err) => err);
		  // if (err || res.errMsg !== "login:ok") return uni.$showMsg("登录失败！");
		
		  uni.setStorageSync("info", info.userInfo);
		  console.log("res >>", res);
		  console.log("info >>",info);
		  const data = await this.$u.post("api/auth/login", {
		    code: res.code,
		    nickName: info.userInfo,
			picture: info.avatarUrl,
		  });
		  console.log('data',data)
		  if(data.code != 1) return this.$tools.msg(res.msg)
		  this.$tools.msg(res.msg)
		  uni.setStorageSync('uid',data.data.uid)
		  uni.setStorageSync('openid',data.data.openid)
		  uni.navigateTo({
		  	url: '/pages/tab/home'
		  })
		
		//   const data1 = await this.$u.post("api/set/user_info", {
		//     info: info.userInfo,
		//     openID: data.data,
		//   });
		
		//   if (!data1.code) return uni.$showMsg("登录失败");
		//   uni.setStorageSync("uid", data1.data);
		
		//   // reLaunch 重新启动
		//   uni.reLaunch({
		//     url: "../index/index",
		//   });
		//   uni.$showMsg("用户登录成功！");
		},
		
		closeAuth() {
			uni.navigateBack({
				url: '/pages/tab/index'
			})
		}
	}
};
</script>

<style lang="scss">
// 登录提示
.modal-box {
	width: 610rpx;
	border-radius: 20rpx;
	background: #fff;
	position: relative;
	left: 50%;
	transform: translateX(-50%);
	padding-bottom: 30rpx;

	.head-bg {
		width: 100%;
		height: 210rpx;
	}

	.detail {
		.title1 {
			color: #46351b;
			font-size: 35rpx;
			font-weight: bold;
		}

		.title2 {
			font-size: 28rpx;
			color: #999;
			padding-top: 20rpx;
		}
	}

	.btn-box {
		margin-top: 80rpx;

		.login-btn {
			width: 492rpx;
			height: 70rpx;
			background: linear-gradient(90deg, rgba(233, 180, 97, 1), rgba(238, 204, 137, 1));
			box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
			border-radius: 35rpx;
			font-size: 28rpx;
			color: rgba(#fff, 0.9);
		}

		.close-btn {
			width: 492rpx;
			height: 70rpx;
			color: #e9b766;
			font-size: 26rpx;
			margin-top: 20rpx;
			background: none;
		}
	}
}

// 小程序登录提醒
/* #ifdef MP-WEIXIN */
.force-login-wrap {
	position: fixed;
	width: 100vw;
	height: 100vh;
	overflow: hidden;
	z-index: 11111;
	top: 0;
	background: linear-gradient(180deg, #2980B9 0%, #6DD5FA 98%);
	.logo-bg {
		width: 640rpx;
		height: 300rpx;
	}
	.force-login__content {
		position: absolute;
		left: 50%;
		top: 50%;
		transform: translate(-50%, -50%);
		.user-avatar {
			width: 160rpx;
			height: 160rpx;
			border-radius: 50%;
			overflow: hidden;
			margin-bottom: 40rpx;
		}
		.user-name {
			font-size: 35rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #FFFFFF;
			margin-bottom: 30rpx;
		}
		.login-notice {
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 400;
			color: #FFFFFF;
			line-height: 44rpx;
			width: 400rpx;
			text-align: center;
			margin-bottom: 80rpx;
		}
		.author-btn {
			width: 630rpx;
			height: 80rpx;
			background: linear-gradient(180deg, #2980B9, #6DD5FA);
			box-shadow: 0px 7rpx 6rpx 0px rgba(229, 138, 0, 0.22);
			border-radius: 40rpx;
			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: rgba(255, 255, 255, 1);
		}
		.close-btn {
			width: 630rpx;
			height: 80rpx;
			margin-top: 30rpx;
			border-radius: 40rpx;
			border: 2rpx solid #2980B9;
			background: none;
			font-size: 30rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #FFFFFF;
		}
	}
}
/* #endif */
</style>
